package com.example.blog.service;

import com.example.blog.mapper.BlogMapper;
import com.example.blog.mapper.UserInfoMapper;
import com.example.blog.model.BlogInfo;
import com.example.blog.model.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class UserService {
    @Autowired
    UserInfoMapper userInfoMapper;

    @Autowired
    private BlogMapper blogMapper;


    public UserInfo selectByName(String userName) {
        return userInfoMapper.selectByName(userName);
    }

    public UserInfo selectById(Integer userId) {
        return userInfoMapper.selectById(userId);
    }

    public UserInfo getAuthorInfo(Integer blogId) {
        // 1.根据博客id, 获取作者id
        // 2.根据作者id, 获取作者信息
        BlogInfo blogInfo = blogMapper.selectById(blogId);
        if(blogInfo == null && blogInfo.getUserId() <= 0) {
            log.error("博客不存在或者信息不合法! blogId:{}", blogId);
            return null;
        }
        UserInfo userInfo = userInfoMapper.selectById(blogInfo.getUserId());
        if(userInfo == null) {
            log.info("无相关作者信息");
        }
        return userInfo;
    }

    public Boolean insertUserInfo(UserInfo userInfo) {
        Integer res = userInfoMapper.insertUserInfo(userInfo);
        if(res > 0) {
            return true;
        }
        return false;
    }
}
